from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from myapp.models import Login, Journalism
from myapp.ser import LoginSer, JournalismSer


# Create your views here.


class LoginAPIView(APIView):
    def get(self, request):
        user = request.data.get('name')
        password = request.data.get('password')
        username = Login.objects.filter(name=user).first()
        pd = Login.objects.filter(password=password).first()
        if user == username:
            if pd == password:
                return Response({'code': 200, 'msg': '登陆成功'})


class JournalismAPIView(APIView):
    def get(self, request):
        id = request.GET.get('id')
        if not id:
            data = Journalism.objects.all()
            ser = JournalismSer(data, many=True)
            return Response(ser.data)
        else:
            data = Journalism.objects.get(pk=id)
            ser = JournalismSer(data)
            return Response(ser.data)
